package cn.edu.laud.sany.datatest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

import oracle.net.aso.l;

import cn.edu.laud.sany.types.ImportProcess;
import cn.edu.laud.sany.types.RowData;
import cn.edu.laud.sany.types.TimeSerial;
import cn.edu.laud.sany.utils.Config;
import cn.edu.laud.sany.utils.MyLog;
import cn.edu.laud.sany.utils.ProcessManager;

public class ScanDataOfDate
{	
	protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	protected static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
		
	public static long allReadCount = 0;
	public static long[] allDataCount;
	public static BufferedWriter writer1;
	
	/**
	 * @param args
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException
	{
		doMain(args);
	}
	
	protected static void doMain(String[] args) throws ClassNotFoundException, SQLException,
			IOException
	{
		try
		{
			writer1 = new BufferedWriter(new FileWriter(new File("result.txt")));
			allDataCount = new long[24];
			
			long starttime = System.nanoTime();
			
			BufferedReader bReader = new BufferedReader(new FileReader(args[0]));
			String lineData = bReader.readLine();
			while ((lineData = bReader.readLine()) != null)
			{
				process(lineData);
				allReadCount++;
				if (allReadCount % 1000000 == 0)
				{
					System.out.println("read count=" + allReadCount);
				}
			}
			System.out.println("read count=" + allReadCount);
			bReader.close();
			
			long endTime = System.nanoTime();
			
			for (int i = 0; i < 24; i++)
			{
				writer1.write(i+":"+allDataCount[i]);
				writer1.write("\r\n");			
			}
			
			writer1.flush();
			writer1.close();
			endTime = System.nanoTime();
			System.out.println("program's run time:"
					+ cn.edu.laud.sany.utils.TimeUtils.nanoSecondToString((endTime - starttime)));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	protected static void process(String string)
	{
		String data[] = null;
		try
		{
			data = string.split(",");
			if (data.length != 6)
			{
				System.err.println("data format error.(org:)" + string);
				return;
			}
			Date localTime;
			String dataString = trimData(data[5]);
			try
			{
				localTime = sdf.parse(dataString);
			}
			catch (ParseException e)
			{
				try
				{
					localTime = sdf2.parse(dataString);
				}
				catch (ParseException e1)
				{
					e1.printStackTrace();
					System.err.println(e1.getMessage() + "(ori:)" + string);
					return;
				}
			}
			int hour = localTime.getHours();
			allDataCount[hour]++;
			
		}
		catch (NumberFormatException e1)
		{
			e1.printStackTrace();
			System.err.println(e1.getMessage() + "(ori:)" + string);
			return;
		}
	}
	
	private static String trimData(String oriData)
	{
		oriData = oriData.trim();
		oriData = oriData.substring(1, oriData.length() - 1);
		return oriData;
	}
	
}
